package com.cn.demo.service;

import com.cn.demo.config.RabbitConst;
import com.cn.demo.config.SeckillLockUserCache;
import com.cn.demo.entity.SeckillGoods;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 秒杀业务类
 *
 * @author wangjf
 * @version 1.0
 * @createDate 2019/11/14 16:37
 * @projectName test
 */
@Service
public class SeckillService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SeckillLockUserCache seckillLockUserCache;

    /**
     * 秒杀商品
     *
     * @param goods
     * @param userId
     * @param queueId
     * @return java.lang.String
     * @author: wangjf
     * @createTime: 2019/11/21 15:08
     */
    public String seckillGoods(SeckillGoods goods, String userId, String queueId) {

        goods.setLockUserId(userId);
        seckillLockUserCache.put(queueId + "_" + userId, goods.getId());
        rabbitTemplate.convertAndSend(RabbitConst.SECKILL_EXCHANGE, RabbitConst.SECKILL_ROUTING_KEY, goods);
        return goods.getId();
    }


}
